<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            position: absolute;
            top: 100px;
            left: 100px;
            width: 300px;
            height: 200px;
            background-color: #fff;
            border: 1px solid #bbb;
            z-index: 9;
        }

        .mask {
            display: none;
            width: 100%;
            height: 100vh;
            background-color: #eee;
        }

        .title {
            height: 50px;
            background-color: skyblue;
            color: #fff;
            text-align: center;
            line-height: 50px;
            cursor: move;
        }
    </style>
</head>

<body>

    <div class="box">
        <div class="title">拖动区域
        </div>
        <div class="content"></div>
    </div>
    <div class="mask"></div>

    <script>

        var box = document.getElementsByClassName('box')[0];
        var mask = document.getElementsByClassName('mask')[0];

        //显示隐藏遮罩层
        box.addEventListener('mouseenter', function () {
            mask.style.display = 'block';
        })
        box.addEventListener('mouseleave', function () {
            mask.style.display = 'none';
        })


        //第二种
        //鼠标拖曳
        var title = document.getElementsByClassName('title')[0];
        title.onmousedown = function (e) {
            let x = e.offsetX; //鼠标指针盒子里的水平坐标
            let y = e.offsetY;

            // 清除文字选中
            window.getSelection().removeAllRanges();
            document.onmousemove = function (e) {
                box.style.left = e.pageX - x + 'px';//拉了多少距离
                box.style.top = e.pageY - y + 'px';
            }

        }
        //弹起移出移动监听器
        document.addEventListener('mouseup', function () {
            document.onmousemove = null;
        })


        //第一种
        // title.addEventListener('mousedown', function (e) {
        //     // let x = e.pageX - box.offsetLeft; //鼠标指针盒子里的水平坐标
        //     // let y = e.pageY - box.offsetTop;
        //     let x = e.offsetX; //鼠标指针盒子里的水平坐标
        //     let y = e.offsetY;
        //     // console.log(x, y);

        //     // 清除文字选中
        //     window.getSelection().removeAllRanges();

        //     //在document内移动
        //     // document.addEventListener('mousemove', move)
        //     // function move(e) {
        //     //     box.style.left = e.pageX - x + 'px';//拉了多少距离
        //     //     box.style.top = e.pageY - y + 'px';
        //     // }
           
        //     //弹起移出移动监听器
        //     document.addEventListener('mouseup', function () {
        //         document.removeEventListener('mousemove', move);
        //     })
        // })




    </script>


</body>

</html>